package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;

/**
 * @author Jason Pratt
 */
public class StringPropertyViewController extends TextFieldEditablePropertyViewController {
	protected javax.swing.JLabel stringLabel = new javax.swing.JLabel();
	protected boolean emptyStringWritesNull;
	private java.awt.Dimension minSize = new java.awt.Dimension( 20, 16 );

	public StringPropertyViewController() {
		stringLabel.setMinimumSize( minSize );
		textField.setColumns( 20 );
	}

	public void set( edu.cmu.cs.stage3.alice.core.Property property, boolean allowExpressions, boolean omitPropertyName, final edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory ) {
		set( property, allowExpressions, omitPropertyName, true, factory );
	}

	//Rem @Override
	public void set( edu.cmu.cs.stage3.alice.core.Property property, boolean includeDefaults, boolean allowExpressions, boolean omitPropertyName, boolean emptyStringWritesNull, final edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory ) {
		super.set( property, includeDefaults, allowExpressions, true, omitPropertyName, factory );
		if( edu.cmu.cs.stage3.alice.core.response.Comment.class.isAssignableFrom( property.getOwner().getClass() ) || edu.cmu.cs.stage3.alice.core.question.userdefined.Comment.class.isAssignableFrom( property.getOwner().getClass() ) ) {
			stringLabel.setForeground( AuthoringToolResources.getColor( "commentForeground" ) );
			int fontSize = 12;
			stringLabel.setFont( new java.awt.Font( "Helvetica", java.awt.Font.BOLD, (int)fontSize) );
		} else {
			stringLabel.setForeground( javax.swing.UIManager.getColor( "Label.foreground" ) );
			stringLabel.setFont( javax.swing.UIManager.getFont( "Label.font" ) );
		}
		refreshGUI();
	}

	public void set( edu.cmu.cs.stage3.alice.core.Property property, boolean allowExpressions, boolean omitPropertyName, boolean emptyStringWritesNull, final edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory ) {
		set( property, false, allowExpressions, omitPropertyName, emptyStringWritesNull, factory );
	}

	//Rem @Override
	protected void setValueFromString( String valueString ) {
		if( valueString.trim().equals( "" ) ) {
			if( emptyStringWritesNull ) {
				valueString = null;
			}
		}
		((Runnable)factory.createItem( valueString )).run();
	}

	//Rem @Override
	protected java.awt.Component getNativeComponent() {
		return stringLabel;
	}

	//Rem @Override
	protected Class getNativeClass() {
		return String.class;
	}

	//Rem @Override
	protected void updateNativeComponent() {
		stringLabel.setText( property.get().toString() );
	}

	//Rem @Override
	protected void refreshGUI() {
		if( (property.getValue() != null) && (property.getValue().toString()).trim().equals( "" ) ) {
			stringLabel.setPreferredSize( minSize );
		} else {
			stringLabel.setPreferredSize( null );
		}
		if( this.isAncestorOf( textField ) ) {
			remove( textField );
		}
		super.refreshGUI();
	}
}
